//
//  NTFOFlashableOutlineView.m
//  
//	Copyright (c) 2009 Jim Turner <jim@nukethemfromorbit.com>
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//
//	http://www.opensource.org/licenses/mit-license.php
//

#import "NTFOFlashableTableView.h"
#import "NSBezierPath-Extensions.h"
#import "NSColor-Extensions.h"

NSString* const kFlashRowHighlightFrame			= @"FlashRowHighlightFrame";
NSString* const kFlashRowCellFrame				= @"FlashRowCellFrame";
NSString* const kFlashRowTargetRow				= @"FlashRowTargetRow";
NSString* const kFlashRowShouldDrawHighlight	= @"FlashRowShouldDrawHighlight";
NSString* const kFlashRowDrawCounter			= @"FlashRowDrawCounter";

#define kFlashTimerInterval			0.08
#define kNumberOfTimesToCycleFlash	10

@implementation NTFOFlashableOutlineView (NTFOFlashableOutlineView_Private)
-(void) updateFlasher:(NSTimer *)aTimer;
@end

@implementation NTFOFlashableOutlineView

-(void) flashReceiptAtRow:(NSInteger)tgtRow
{
	NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:NSStringFromRect( [self rectOfRow:tgtRow] ), kFlashRowHighlightFrame, 
																					  NSStringFromRect( [self frameOfCellAtColumn:0 row:tgtRow] ), kFlashRowCellFrame, 
																					  [NSNumber numberWithInteger:tgtRow], kFlashRowTargetRow, 
																					  [NSNumber numberWithBool:YES], kFlashRowShouldDrawHighlight, 
																					  [NSNumber numberWithInteger:0], kFlashRowDrawCounter,
																					  nil];
	
	// Just in case the user gets another of these fired off before we're finished
	if( _flashTimer && [_flashTimer isValid] )
	{
		[_flashTimer invalidate];
		[_flashTimer autorelease];
	}
	
	_flashTimer = [[NSTimer scheduledTimerWithTimeInterval:kFlashTimerInterval target:self selector:@selector( updateFlasher: ) userInfo:userInfo repeats:YES] retain];
}

-(void) updateFlasher:(NSTimer *)aTimer
{
	NSMutableDictionary *userInfo = [aTimer userInfo];
	
	BOOL shouldDraw = [[userInfo objectForKey:kFlashRowShouldDrawHighlight] boolValue];
	[userInfo setObject:[NSNumber numberWithBool:!shouldDraw] forKey:kFlashRowShouldDrawHighlight];
	
	NSInteger drawCounter = [[userInfo objectForKey:kFlashRowDrawCounter] integerValue];
	[userInfo setObject:[NSNumber numberWithInteger:++drawCounter] forKey:kFlashRowDrawCounter];
	
	if( [[userInfo objectForKey:kFlashRowDrawCounter] integerValue] >= kNumberOfTimesToCycleFlash )
	{
		[_flashTimer invalidate];
		[_flashTimer autorelease];
		_flashTimer = nil;
	}
	
	[self setNeedsDisplayInRect:NSRectFromString( [userInfo objectForKey:kFlashRowHighlightFrame] )];
}

-(void) drawRect:(NSRect)frame
{
	[super drawRect:frame];
	
	// flashing drawing must come after super drawRect: otherwise it'll blit over our drawing with the background color
	if( _flashTimer && [_flashTimer isValid] && [[[_flashTimer userInfo] objectForKey:kFlashRowShouldDrawHighlight] boolValue] )
	{
		NSRect _flashRect = NSRectFromString( [[_flashTimer userInfo] objectForKey:kFlashRowHighlightFrame] );
		_flashRect = NSInsetRect( _flashRect, 2.0, 1.0 );
		
		NSBezierPath *path = [NSBezierPath roundRectPath:_flashRect withCornerRadius:6.0 roundMinXMinY:YES roundMaxXMinY:YES roundMinXMaxY:YES roundMaxXMaxY:YES];
		[path setLineWidth:2.0];
		[[NSColor genericColorWith8BitRed:198.0 green:214.0 blue:242.0 alpha:1.0] set];
		[path fill];
		
		[[NSColor genericColorWith8BitRed:61.0 green:89.0 blue:211.0 alpha:1.0] set];
		[path stroke];
		
		NSRect _cellRect = NSRectFromString( [[_flashTimer userInfo] objectForKey:kFlashRowCellFrame] );
		NSInteger targetRow = [[[_flashTimer userInfo] objectForKey:kFlashRowTargetRow] integerValue];
		
		NSCell *cell = [self preparedCellAtColumn:0 row:targetRow];
		[cell drawWithFrame:_cellRect inView:self];
	}
}


@end
